package com.xea.service;

import java.util.List;

import com.uf.util.SqlHelper;
import com.xea.model.Channel;

public class ChannelService {

	/**
	 * 新增类别
	 * @param channel   类别实体
	 * @return
	 */
	public void add(Channel channel) throws Exception {
		
		String sql = "insert into Channel(name,type,parentId,remark) values(?,?,?,?)";
		Object[] params=new Object[]{channel.getName(),channel.getType(),channel.getParentId(),channel.getRemark()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 查询所有类别
	 * @param channel   类别实体
	 * @return
	 */
	public List<Channel> search() throws Exception {
		
		String sql = "select * from Channel";
		return SqlHelper.query(sql, null, Channel.class);
		
	}
	
	/**
	 * 查询所有父类别
	 * @param channel   类别实体
	 * @return
	 */
	public List<Channel> search(int parentid) throws Exception {
		
		String sql = "select * from Channel where parentId="+parentid;
		return SqlHelper.query(sql, null, Channel.class);
		
	}
	
	/**
	 * 类别详情
	 * @param id   类别id
	 * @return
	 */
	public Channel findById(int id){
		
		String sql = "select * from Channel where id = ?";		
		List<Channel> list = SqlHelper.query(sql, new Object[]{id}, Channel.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 删除类别
	 * @param id   类别id
	 * @return
	 */
	public void del(String id){
		
		String sql = "delete from Channel where id in("+id+")";
		SqlHelper.excute(sql, null);
		
	}
	
	/**
	 * 修改类别
	 * @param Channel   类别实体
	 * @return
	 */
	public void update(Channel channel){
		
		String sql = "update Channel set name=?,remark=? where id=?";
		Object[] params=new Object[]{channel.getName(),channel.getRemark(),channel.getId()};
		SqlHelper.excute(sql, params);
	}
	
	/**
	 * 查询所属类别下类别名是否存在
	 * @param name  类别名
	 * @param parentId  父id
	 * @return
	 */
	public Channel find(int parentId,String name){
		
		String sql = "select * from Channel where name = ? and parentId=?";
		Object[] params = new Object[]{name,parentId};
		List<Channel> ls =SqlHelper.query(sql, params, Channel.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (Channel)ls.get(0);		

	}
	
	/**
	 * 查询所属类别下是否还有类别
	 * @param name  类别名
	 * @param parentId  父id
	 * @return
	 */
	public Channel find(int parentId){
		
		String sql = "select * from Channel where parentId=?";
		Object[] params = new Object[]{parentId};
		List<Channel> ls =SqlHelper.query(sql, params, Channel.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (Channel)ls.get(0);		

	}

	/**
	 * 查询类别是否存在（排除当前传入id)
	 * @param name  类别名
	 * @return
	 */
	public Channel find(String name,int id,int parentId){
		
		String sql = "select * from Channel where name = ? and id!=? and parentId=? ";
		Object[] params = new Object[]{name,id,parentId};
		List<Channel> ls =SqlHelper.query(sql, params, Channel.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (Channel)ls.get(0);		

	}
}
